<?php

	if(is_numeric($this->getParams()))
	{
		try
		{
			$post = PostMapper::get()->find($this->getParams());
			$this->view()->assignRef('post', $post);

			$post_plugin = Plugin::load('post', false);
			$this->view()->assignRef('post_plugin', $post_plugin);
		}
		catch(FrameworkRecordNotFoundException $e)
		{
			// go home
			$this->setGuiMode('ajax');
			header("Location: /blog");
			die();
		}

		$comments = CommentMapper::get()->findByPost($post);
		$this->view()->assignRef('comments', $comments);

		$comment_plugin = Plugin::load('comment');
		$this->view()->assignRef('comment_plugin', $comment_plugin);

		// spam management
		include_once('app/img/spam_pics/spam_image_map.php');


		shuffle($fluffy_images);
		$chosen_fluffy = array_slice($fluffy_images, 0, 4);

		shuffle($regular_images);
		$chosen_regular = array_slice($regular_images, 0, 2);

		$images = array_merge($chosen_fluffy, $chosen_regular);
		shuffle($images);

		$this->view()->assign('spam_check_pics', $images);

		$this->addInclude('app/css/areas/post.css','css');
	}
	elseif(preg_match('/^(\d+)\/rss$/', $this->getParams(), $matches))
	{
		$this->setGuiMode('ajax');
		try
        {
        	$post = PostMapper::get()->find($matches[1]);
        }
        catch(FrameworkRecordNotFoundException $e)
        {
        	header('Location: /blog');
        	die();
        }
        $comments = CommentMapper::get()->findByPost($post);
        $this->view()->assignRef('comments', $comments);
        $this->view()->assignRef('post', $post);
		$this->view()->assign('template', 'post/rss.tpl');
	}
	else
	{
		// go home
		$this->setGuiMode('ajax');
		header("Location: /blog");
		die();
	}
?>